I'm trying to build a TF model which takes a tensor with [1,3] shape
// example: [1,2,3]
And the output is the same
here's a simple code to reproduce the error
const {
tensor1d,
sequential,
layers,
} = require('@tensorflow/tfjs-node');
const model = sequential();
model.add(
layers.dense({
units: 3,
inputShape: [3],
})
);
model.compile({
loss: 'meanSquaredError',
optimizer: 'adam',
});
let inputTensor = tensor1d([1,2,3]);
let outputTensor = tensor1d([1,2,3]);
model.fit(inputTensor, outputTensor);
let result = model.predict(inputTensor); // it fires the error here
The model.fit & model.predict functions both take the same input shape from inputTensor variable, however it fires error in the second function model.predict
Any explanation for that error, I'm a bit newbie in Tensorflow
When you feed data into tensorflow model, remember the shape of input data should have batch dimension.
So your input data have to expand the dimension, like this:
let inputTensor = tensor1d([[1,2,3]]);
let outputTensor = tensor1d([[1,2,3]]);
Or use tensorflow function tf.expandDims to do this :
The shape of output and input tensor will be (1,3).
The error message means you feed 3 data with 1 dimension, but model expect 3 dimension.